package prefetch

import (
	"encoding/json"
	"os"
	"sync"
)

type Store struct {
	mu       sync.Mutex
	filePath string
	Data     map[string]int
}

func NewStore(path string) *Store {
	return &Store{filePath: path, Data: make(map[string]int)}
}

func (s *Store) Load() error {
	data, err := os.ReadFile(s.filePath)
	if err != nil {
		return nil
	}
	return json.Unmarshal(data, &s.Data)
}

func (s *Store) Save() error {
	s.mu.Lock()
	defer s.mu.Unlock()
	data, _ := json.MarshalIndent(s.Data, "", "  ")
	return os.WriteFile(s.filePath, data, 0644)
}
